import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description: 给定一个有序整型数组, 实现二分查找
 * User: 15085202591
 * Date: 2022-11-05
 * Time: 10:23
 */
public class BinarySearch {
    public static int binary(int[] array,int n){
        int left = 0;
        int right = array.length - 1;
        int mid = 0;
        while (left <= right){
            mid = (left + right) >> 1;
            if(n < array[mid]){
                right = mid - 1;
            }else if (n > array[mid]){
                left = mid + 1;
            }else {
                return mid;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        int[] array = new int[]{1,2,3,4,5,8,12,23,45,56};
        System.out.println(Arrays.toString(array));
        System.out.println("查找"+23);
        int ret = binary(array,23);
        System.out.println(ret);
    }
}
